namespace StrongNameRemove
{
    using StrongNameRemove.Properties;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;

    public class MainForm : Form
    {
        private static global::System.Resources.ResourceManager resourceMan;
        private FolderBrowserDialog folderBrowserDialog;
        private Button buttonDirRm;
        private TextBox textBox1;
        private Label asmprefix;
        private Label label2;
        private Label label1;
        private GroupBox groupBox1;

        private static global::System.Globalization.CultureInfo resourceCulture;

        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal MainForm()
        {
        }

        /// <summary>
        ///   返回此类使用的缓存的 ResourceManager 实例。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        public static global::System.Resources.ResourceManager ResourceManager
        {
            get
            {
                if (object.ReferenceEquals(resourceMan, null))
                {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("StrongNameRemove.MainForm", typeof(MainForm).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }

        /// <summary>
        ///   使用此强类型资源类，为所有资源查找
        ///   重写当前线程的 CurrentUICulture 属性。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        public static global::System.Globalization.CultureInfo Culture
        {
            get
            {
                return resourceCulture;
            }
            set
            {
                resourceCulture = value;
            }
        }

        /// <summary>
        ///   查找 System.Windows.Forms.ImageListStreamer 类型的本地化资源。
        /// </summary>
        public static System.Windows.Forms.ImageListStreamer imageListResult_ImageStream
        {
            get
            {
                object obj = ResourceManager.GetObject("imageListResult.ImageStream", resourceCulture);
                return ((System.Windows.Forms.ImageListStreamer)(obj));
            }
        }

        public static System.Drawing.Icon MainIcon
        {
            get
            {
                object obj = ResourceManager.GetObject("$this.Icon", resourceCulture);
                return ((System.Drawing.Icon)(obj));
            }
        }
        private uint assemblyFlag;
        private long assemblyFlagOffset;
        private ArrayList assemblyReferences = new ArrayList();
        private int blobIndexSize;
        private Button btnBrowse;
        private Button btnPatch;
        private Button btnPatchReferences;
        private Button btnVerify;
        private CheckBox chkBackup;
        private uint cliHeaderFlag;
        private long cliHeaderFlagOffset;
        private ColumnHeader columnHeader;
        private ColumnHeader columnHeaderOffset;
        private ColumnHeader columnHeaderPublicKeyOrToken;
        private ColumnHeader columnHeaderReferenceName;
        private ColumnHeader columnHeaderReferenceVersion;
        private string compiledRuntimeVersion = string.Empty;
        private IContainer components;
        private GroupBox grpAbout;
        private ImageList imageListResult;
        private Label lblBy;
        private Label lblFileName;
        private Label lblVersion;
        private LinkLabel lnkAuthor;
        private LinkLabel lnkCredits;
        private ListView lstReferences;
        private ListView lstResults;
        private OpenFileDialog openFileDialog;
        private PictureBox pcbIcon;
        private long publicKeyIndexOffset;
        private long publicKeyOffset;
        private long strongNameSignatureOffset;
        private ToolTip toolTip;
        private TextBox txtFileName;

        public MainForm(string fileName)
        {
            this.InitializeComponent();
            if (!string.IsNullOrEmpty(fileName))
            {
                this.txtFileName.Text = fileName;
            }
        }

        private void AppendMessage(string message, ImageListIcon icon)
        {
            this.lstResults.Items.Add(new ListViewItem(message, (int)icon));
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            this.openFileDialog.CheckFileExists = true;
            this.openFileDialog.DefaultExt = "exe";
            this.openFileDialog.Filter = "Assemblies files (*.exe, *.dll)|*.exe;*.dll|All files (*.*)|*.*";
            if (this.txtFileName.Text.Trim().Length > 0)
            {
                this.openFileDialog.InitialDirectory = Path.GetDirectoryName(this.txtFileName.Text);
            }
            this.openFileDialog.Multiselect = false;
            this.openFileDialog.Title = "Select assembly file to patch";
            this.openFileDialog.FileName = string.Empty;
            if (this.openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.txtFileName.Text = this.openFileDialog.FileName;
                this.Verify();
            }
        }

        public static void ListFiles(FileSystemInfo info, ref ArrayList fileList)
        {
            if (!info.Exists) return;

            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录 
            if (dir == null) return;

            FileSystemInfo[] files = dir.GetFileSystemInfos();
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;
                //是文件 
                if (file != null)
                {

                    if (file.FullName.EndsWith(".dll") ||
                        file.FullName.EndsWith(".exe"))
                    {
                        fileList.Add(file.FullName);
                    }
                }
                //对于子目录，进行递归调用 
                else
                    ListFiles(files[i], ref fileList);
            }
        }

        private void btnDirRm_Click(object sender, EventArgs e)
        {
            DialogResult ret = this.folderBrowserDialog.ShowDialog();
            if (ret == DialogResult.OK)
            {
                this.AppendMessage("开始去除文件夹内所有强名", ImageListIcon.Info);
                ArrayList fileList = new ArrayList();
                DirectoryInfo dir = new DirectoryInfo(this.folderBrowserDialog.SelectedPath);
                ListFiles(dir, ref fileList);
                bool bOK = false;
                string name = "";
                foreach (string fileName in fileList)
                {
                    name = Path.GetFileNameWithoutExtension(fileName);
                    if (name.StartsWith("xtest") ||
                        name.StartsWith("parasoft") ||
                        name.StartsWith("cpptest"))
                    {
                        ArrayList asmRefs = new ArrayList();
                        if (Utility.GetAssemblyData(fileName, ref this.cliHeaderFlag, ref this.cliHeaderFlagOffset,
                            ref this.strongNameSignatureOffset, ref this.publicKeyIndexOffset, ref this.publicKeyOffset,
                            ref this.assemblyFlag, ref this.assemblyFlagOffset, ref this.compiledRuntimeVersion,
                            ref asmRefs, ref this.blobIndexSize))
                        {
                            if (0 == PatchAsmSS(fileName, false))
                            {
                                if (PatchRefs(fileName, ref asmRefs) == 0)
                                {
                                    bOK = true;
                                }
                            }
                        }
                    }
                }
                if (bOK)
                {
                    this.AppendMessage("去除文件夹内所有强名： 成功", ImageListIcon.OK);
                    MessageBox.Show("去除文件夹内所有强名： 成功");
                }
                else
                {
                    this.AppendMessage("去除文件夹内所有强名： 失败", ImageListIcon.Error);
                    MessageBox.Show("去除文件夹内所有强名： 失败");
                }
            }
        }

        private void btnPatch_Click(object sender, EventArgs e)
        {
            string fileName = this.txtFileName.Text.Trim();
            int err = PatchAsmSS(fileName, this.chkBackup.Checked);
            switch(err)
            {
                case 0:
                    {
                        this.AppendMessage("CLI Header Strong Name flag reset to unsigned", ImageListIcon.OK);
                        this.AppendMessage("Strong Name signature offset reset to 0x00", ImageListIcon.OK);
                        this.AppendMessage("Public Key index in Assembly Table reset to 0x00", ImageListIcon.OK);
                        this.AppendMessage("Flags in Assembly Table reset to unsigned", ImageListIcon.OK);
                        this.btnPatch.Enabled = false;
                    }
                    break;
                case 1:
                this.AppendMessage("请选择有效的程序集文件", ImageListIcon.Alert);
                    break;
                case 2:
                this.AppendMessage("文件没找到或不能写入: " + fileName, ImageListIcon.Error);
                    break;
                case 3:
                    this.AppendMessage("Unable to open assembly or not a valid .NET assembly", ImageListIcon.Error);
                    break;
                case 4:
                    this.AppendMessage("Assembly doesn't seem to be strong signed. Nothing to do", ImageListIcon.Alert);
                    break;
                default:
                    break;
            }
        }

        private int PatchAsmSS(string fileName, bool bBackUp)
        {
            if (fileName.Length == 0)
            {
                return 1;
            }
            else if (!Utility.CheckFileExistsAndWriteable(fileName))
            {
                return 2;
            }
            else
            {
                string destFileName = Path.GetDirectoryName(fileName) + @"\Backup of (" + DateTime.Now.ToString("yyyyMMddHHmm") + ") - " + Path.GetFileName(fileName);
                if (bBackUp)
                {
                     File.Copy(fileName, destFileName, true);
                }
                if (Utility.IsAssemblyStrongSigned(this.cliHeaderFlag))
                {
                    if (Utility.PatchAssemblyStrongSigning(fileName, this.cliHeaderFlag, this.cliHeaderFlagOffset, this.strongNameSignatureOffset, this.publicKeyIndexOffset, this.assemblyFlag, this.assemblyFlagOffset, this.blobIndexSize))
                    {
                        return 0;
                    }
                    else
                    {
                        return 3;
                    }
                }
                else
                {
                    if (bBackUp)
                    {
                        File.Delete(destFileName);
                    }
                    return 0;
                }
            }
        }

        private void btnPatchReferences_Click(object sender, EventArgs e)
        {
            string fileName = this.txtFileName.Text.Trim();
            int err = PatchRef(fileName);
        }

        private int PatchRef(string fileName)
        {
            if (fileName.Length == 0)
            {
                this.AppendMessage("Please choose a valid filename", ImageListIcon.Alert);
            }
            else if (!Utility.CheckFileExistsAndWriteable(fileName))
            {
                this.AppendMessage("File not found or not writable: " + fileName, ImageListIcon.Error);
            }
            else
            {
                string destFileName = Path.GetDirectoryName(fileName) + @"\Backup of (" + DateTime.Now.ToString("yyyyMMddHHmm") + ") - " + Path.GetFileName(fileName);
                if (this.chkBackup.Checked)
                {
                    File.Copy(fileName, destFileName, true);
                }
                bool flag = false;
                foreach (ListViewItem item in this.lstReferences.CheckedItems)
                {
                    if (((AssemblyReference)item.Tag).PublicKeyOrToken.Length > 0)
                    {
                        if (Utility.PatchReference(fileName, ((AssemblyReference)item.Tag).ReferenceOffset, this.blobIndexSize))
                        {
                            this.AppendMessage("PublicKeyOrToken removed correctly from " + ((AssemblyReference)item.Tag).ReferenceName + " reference", ImageListIcon.OK);
                            flag = true;
                            item.Checked = false;
                            this.btnPatchReferences.Enabled = false;
                        }
                        else
                        {
                            this.AppendMessage("Error removing PublicKeyOrToken from " + ((AssemblyReference)item.Tag).ReferenceName + " reference", ImageListIcon.Error);
                        }
                    }
                    else
                    {
                        this.AppendMessage("PublicKeyOrToken not available on reference " + ((AssemblyReference)item.Tag).ReferenceName + ". Nothing to do", ImageListIcon.Alert);
                        item.Checked = false;
                    }
                    this.Verify();
                }
                if (!flag && this.chkBackup.Checked)
                {
                    File.Delete(destFileName);
                }
            }
            return 0;
        }

        private int PatchRefs(string fileName, ref ArrayList asmRefs)
        {
            if (fileName.Length == 0)
            {
                //this.AppendMessage("Please choose a valid filename", ImageListIcon.Alert);
            }
            else if (!Utility.CheckFileExistsAndWriteable(fileName))
            {
                //this.AppendMessage("File not found or not writable: " + fileName, ImageListIcon.Error);
            }
            else
            {
                foreach (AssemblyReference item in asmRefs)
                {
                    if (item.ReferenceName.IndexOf("xtest") != -1)
                    {
                        if (item.PublicKeyOrToken.Length > 0)
                        {
                            if (Utility.PatchReference(fileName, item.ReferenceOffset, this.blobIndexSize))
                            {
                                //this.AppendMessage("PublicKeyOrToken removed correctly from " + item.ReferenceName + " reference", ImageListIcon.OK);
                            }
                            else
                            {
                                //this.AppendMessage("Error removing PublicKeyOrToken from " + item.ReferenceName + " reference", ImageListIcon.Error);
                            }
                        }
                        else
                        {
                            //this.AppendMessage("PublicKeyOrToken not available on reference " + item.ReferenceName + ". Nothing to do", ImageListIcon.Alert);
                        }
                    }
                }
            }
            return 0;
        }

        private void btnVerify_Click(object sender, EventArgs e)
        {
            this.Verify();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.lblFileName = new System.Windows.Forms.Label();
            this.txtFileName = new System.Windows.Forms.TextBox();
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            this.btnPatchReferences = new System.Windows.Forms.Button();
            this.chkBackup = new System.Windows.Forms.CheckBox();
            this.btnVerify = new System.Windows.Forms.Button();
            this.btnPatch = new System.Windows.Forms.Button();
            this.lnkAuthor = new System.Windows.Forms.LinkLabel();
            this.pcbIcon = new System.Windows.Forms.PictureBox();
            this.btnBrowse = new System.Windows.Forms.Button();
            this.lnkCredits = new System.Windows.Forms.LinkLabel();
            this.lstResults = new System.Windows.Forms.ListView();
            this.columnHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.imageListResult = new System.Windows.Forms.ImageList(this.components);
            this.lstReferences = new System.Windows.Forms.ListView();
            this.columnHeaderReferenceName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeaderReferenceVersion = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeaderPublicKeyOrToken = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeaderOffset = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.grpAbout = new System.Windows.Forms.GroupBox();
            this.lblBy = new System.Windows.Forms.Label();
            this.lblVersion = new System.Windows.Forms.Label();
            this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            this.buttonDirRm = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.asmprefix = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            ((System.ComponentModel.ISupportInitialize)(this.pcbIcon)).BeginInit();
            this.grpAbout.SuspendLayout();
            this.SuspendLayout();
            // 
            // lblFileName
            // 
            this.lblFileName.AutoSize = true;
            this.lblFileName.Location = new System.Drawing.Point(13, 385);
            this.lblFileName.Name = "lblFileName";
            this.lblFileName.Size = new System.Drawing.Size(77, 12);
            this.lblFileName.TabIndex = 0;
            this.lblFileName.Text = "选译程序集：";
            this.lblFileName.UseMnemonic = false;
            // 
            // txtFileName
            // 
            this.txtFileName.Location = new System.Drawing.Point(104, 380);
            this.txtFileName.Name = "txtFileName";
            this.txtFileName.Size = new System.Drawing.Size(418, 21);
            this.txtFileName.TabIndex = 1;
            this.toolTip.SetToolTip(this.txtFileName, "Assembly file name and path to patch");
            this.txtFileName.TextChanged += new System.EventHandler(this.txtFileName_TextChanged);
            // 
            // toolTip
            // 
            this.toolTip.BackColor = System.Drawing.Color.Azure;
            // 
            // btnPatchReferences
            // 
            this.btnPatchReferences.Enabled = false;
            this.btnPatchReferences.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnPatchReferences.Location = new System.Drawing.Point(313, 439);
            this.btnPatchReferences.Name = "btnPatchReferences";
            this.btnPatchReferences.Size = new System.Drawing.Size(118, 41);
            this.btnPatchReferences.TabIndex = 6;
            this.btnPatchReferences.Text = "修正引用";
            this.toolTip.SetToolTip(this.btnPatchReferences, "删除引用程序集的签名");
            this.btnPatchReferences.UseVisualStyleBackColor = true;
            this.btnPatchReferences.Click += new System.EventHandler(this.btnPatchReferences_Click);
            // 
            // chkBackup
            // 
            this.chkBackup.AutoSize = true;
            this.chkBackup.Checked = true;
            this.chkBackup.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkBackup.Location = new System.Drawing.Point(218, 409);
            this.chkBackup.Name = "chkBackup";
            this.chkBackup.Size = new System.Drawing.Size(96, 16);
            this.chkBackup.TabIndex = 3;
            this.chkBackup.Text = "备份原始文件";
            this.toolTip.SetToolTip(this.chkBackup, "修补前备份原始文件");
            this.chkBackup.UseVisualStyleBackColor = true;
            // 
            // btnVerify
            // 
            this.btnVerify.Enabled = false;
            this.btnVerify.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnVerify.Location = new System.Drawing.Point(464, 439);
            this.btnVerify.Name = "btnVerify";
            this.btnVerify.Size = new System.Drawing.Size(106, 41);
            this.btnVerify.TabIndex = 4;
            this.btnVerify.Text = "检验";
            this.toolTip.SetToolTip(this.btnVerify, "读取程序集信息");
            this.btnVerify.UseVisualStyleBackColor = true;
            this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click);
            // 
            // btnPatch
            // 
            this.btnPatch.Enabled = false;
            this.btnPatch.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnPatch.Location = new System.Drawing.Point(218, 439);
            this.btnPatch.Name = "btnPatch";
            this.btnPatch.Size = new System.Drawing.Size(89, 41);
            this.btnPatch.TabIndex = 5;
            this.btnPatch.Text = "修正";
            this.toolTip.SetToolTip(this.btnPatch, "删除程序集上的强命名");
            this.btnPatch.UseVisualStyleBackColor = true;
            this.btnPatch.Click += new System.EventHandler(this.btnPatch_Click);
            // 
            // lnkAuthor
            // 
            this.lnkAuthor.AutoSize = true;
            this.lnkAuthor.Location = new System.Drawing.Point(100, 35);
            this.lnkAuthor.Name = "lnkAuthor";
            this.lnkAuthor.Size = new System.Drawing.Size(107, 12);
            this.lnkAuthor.TabIndex = 2;
            this.lnkAuthor.TabStop = true;
            this.lnkAuthor.Text = "Andrea Bertolotto";
            this.toolTip.SetToolTip(this.lnkAuthor, "Browse to author\'s web site");
            this.lnkAuthor.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkAuthor_LinkClicked);
            // 
            // pcbIcon
            // 
            this.pcbIcon.Image = global::StrongNameRemove.Properties.Resources.System_Locked;
            this.pcbIcon.Location = new System.Drawing.Point(6, 18);
            this.pcbIcon.Name = "pcbIcon";
            this.pcbIcon.Size = new System.Drawing.Size(48, 46);
            this.pcbIcon.TabIndex = 3;
            this.pcbIcon.TabStop = false;
            this.toolTip.SetToolTip(this.pcbIcon, "Strong Name Remove by Andrea Bertolotto");
            // 
            // btnBrowse
            // 
            this.btnBrowse.Image = global::StrongNameRemove.Properties.Resources.folder_explore;
            this.btnBrowse.Location = new System.Drawing.Point(528, 378);
            this.btnBrowse.Name = "btnBrowse";
            this.btnBrowse.Size = new System.Drawing.Size(42, 21);
            this.btnBrowse.TabIndex = 2;
            this.toolTip.SetToolTip(this.btnBrowse, "浏览程序集");
            this.btnBrowse.UseVisualStyleBackColor = true;
            this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
            // 
            // lnkCredits
            // 
            this.lnkCredits.AutoSize = true;
            this.lnkCredits.Location = new System.Drawing.Point(70, 53);
            this.lnkCredits.Name = "lnkCredits";
            this.lnkCredits.Size = new System.Drawing.Size(47, 12);
            this.lnkCredits.TabIndex = 3;
            this.lnkCredits.TabStop = true;
            this.lnkCredits.Text = "Credits";
            this.toolTip.SetToolTip(this.lnkCredits, "Shows credits");
            this.lnkCredits.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkCredits_LinkClicked);
            // 
            // lstResults
            // 
            this.lstResults.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader});
            this.lstResults.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            this.lstResults.Location = new System.Drawing.Point(12, 539);
            this.lstResults.Name = "lstResults";
            this.lstResults.Size = new System.Drawing.Size(402, 59);
            this.lstResults.SmallImageList = this.imageListResult;
            this.lstResults.TabIndex = 8;
            this.lstResults.UseCompatibleStateImageBehavior = false;
            this.lstResults.View = System.Windows.Forms.View.Details;
            // 
            // columnHeader
            // 
            this.columnHeader.Text = "Message";
            this.columnHeader.Width = 2000;
            // 
            // imageListResult
            // 
            this.imageListResult.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListResult.ImageStream")));
            this.imageListResult.TransparentColor = System.Drawing.Color.Transparent;
            this.imageListResult.Images.SetKeyName(0, "remove.png");
            this.imageListResult.Images.SetKeyName(1, "alert.png");
            this.imageListResult.Images.SetKeyName(2, "info.png");
            this.imageListResult.Images.SetKeyName(3, "ok.png");
            // 
            // lstReferences
            // 
            this.lstReferences.CheckBoxes = true;
            this.lstReferences.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeaderReferenceName,
            this.columnHeaderReferenceVersion,
            this.columnHeaderPublicKeyOrToken,
            this.columnHeaderOffset});
            this.lstReferences.GridLines = true;
            this.lstReferences.Location = new System.Drawing.Point(2, 485);
            this.lstReferences.Name = "lstReferences";
            this.lstReferences.Size = new System.Drawing.Size(286, 48);
            this.lstReferences.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.lstReferences.TabIndex = 7;
            this.lstReferences.UseCompatibleStateImageBehavior = false;
            this.lstReferences.View = System.Windows.Forms.View.Details;
            // 
            // columnHeaderReferenceName
            // 
            this.columnHeaderReferenceName.Text = "程序集名";
            this.columnHeaderReferenceName.Width = 206;
            // 
            // columnHeaderReferenceVersion
            // 
            this.columnHeaderReferenceVersion.Text = "程序集版本";
            this.columnHeaderReferenceVersion.Width = 80;
            // 
            // columnHeaderPublicKeyOrToken
            // 
            this.columnHeaderPublicKeyOrToken.Text = "公钥或令牌";
            this.columnHeaderPublicKeyOrToken.Width = 150;
            // 
            // columnHeaderOffset
            // 
            this.columnHeaderOffset.Text = "偏移量";
            this.columnHeaderOffset.Width = 123;
            // 
            // grpAbout
            // 
            this.grpAbout.Controls.Add(this.lblBy);
            this.grpAbout.Controls.Add(this.lnkCredits);
            this.grpAbout.Controls.Add(this.lnkAuthor);
            this.grpAbout.Controls.Add(this.lblVersion);
            this.grpAbout.Controls.Add(this.pcbIcon);
            this.grpAbout.Location = new System.Drawing.Point(2, 404);
            this.grpAbout.Name = "grpAbout";
            this.grpAbout.Size = new System.Drawing.Size(210, 76);
            this.grpAbout.TabIndex = 9;
            this.grpAbout.TabStop = false;
            this.grpAbout.Text = "Strong Name Remove";
            // 
            // lblBy
            // 
            this.lblBy.AutoSize = true;
            this.lblBy.Location = new System.Drawing.Point(62, 35);
            this.lblBy.Name = "lblBy";
            this.lblBy.Size = new System.Drawing.Size(41, 12);
            this.lblBy.TabIndex = 1;
            this.lblBy.Text = "作者：";
            this.lblBy.UseMnemonic = false;
            // 
            // lblVersion
            // 
            this.lblVersion.AutoSize = true;
            this.lblVersion.Location = new System.Drawing.Point(63, 18);
            this.lblVersion.Name = "lblVersion";
            this.lblVersion.Size = new System.Drawing.Size(41, 12);
            this.lblVersion.TabIndex = 0;
            this.lblVersion.Text = "版本：";
            this.lblVersion.UseMnemonic = false;
            // 
            // buttonDirRm
            // 
            this.buttonDirRm.Image = global::StrongNameRemove.Properties.Resources.folder_explore;
            this.buttonDirRm.Location = new System.Drawing.Point(192, 68);
            this.buttonDirRm.Name = "buttonDirRm";
            this.buttonDirRm.Size = new System.Drawing.Size(42, 21);
            this.buttonDirRm.TabIndex = 2;
            this.buttonDirRm.UseVisualStyleBackColor = true;
            this.buttonDirRm.Click += new System.EventHandler(this.btnDirRm_Click);
            // 
            // textBox1
            // 
            this.textBox1.Enabled = false;
            this.textBox1.Location = new System.Drawing.Point(134, 31);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 10;
            this.textBox1.Text = "xtest";
            // 
            // asmprefix
            // 
            this.asmprefix.AutoSize = true;
            this.asmprefix.Location = new System.Drawing.Point(33, 34);
            this.asmprefix.Name = "asmprefix";
            this.asmprefix.Size = new System.Drawing.Size(65, 12);
            this.asmprefix.TabIndex = 11;
            this.asmprefix.Text = "程序集前缀";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(33, 72);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(77, 12);
            this.label2.TabIndex = 12;
            this.label2.Text = "选择目录处理";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(174, 109);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(83, 12);
            this.label1.TabIndex = 13;
            this.label1.Text = "xzfff@126.com";
            // 
            // groupBox1
            // 
            this.groupBox1.Location = new System.Drawing.Point(13, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(244, 85);
            this.groupBox1.TabIndex = 14;
            this.groupBox1.TabStop = false;
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(269, 142);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.asmprefix);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.btnPatchReferences);
            this.Controls.Add(this.lstReferences);
            this.Controls.Add(this.grpAbout);
            this.Controls.Add(this.lstResults);
            this.Controls.Add(this.btnPatch);
            this.Controls.Add(this.btnVerify);
            this.Controls.Add(this.chkBackup);
            this.Controls.Add(this.buttonDirRm);
            this.Controls.Add(this.btnBrowse);
            this.Controls.Add(this.txtFileName);
            this.Controls.Add(this.lblFileName);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "强名去除 v1.0 by zhupf";
            this.Load += new System.EventHandler(this.MainForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pcbIcon)).EndInit();
            this.grpAbout.ResumeLayout(false);
            this.grpAbout.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void lnkAuthor_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.andreabertolotto.net");
        }

        private void lnkCredits_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new CreditsForm().ShowDialog(this);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.lblVersion.Text = "Version: " + Assembly.GetExecutingAssembly().GetName().Version;
        }

        private void txtFileName_TextChanged(object sender, EventArgs e)
        {
            this.btnVerify.Enabled = this.txtFileName.Text.Trim().Length > 0;
            this.btnPatch.Enabled = false;
            this.btnPatchReferences.Enabled = false;
        }

        private void Verify()
        {
            string fileName = this.txtFileName.Text.Trim();
            this.lstResults.Items.Clear();
            this.lstReferences.Items.Clear();
            if (fileName.Length == 0)
            {
                this.AppendMessage("请选择有效的程序集文件" + fileName, ImageListIcon.Alert);
            }
            else if (!Utility.CheckFileExistsAndWriteable(fileName))
            {
                this.AppendMessage("文件没找到或不能写入: " + fileName, ImageListIcon.Error);
            }
            else if (Utility.GetAssemblyData(fileName, ref this.cliHeaderFlag, ref this.cliHeaderFlagOffset, ref this.strongNameSignatureOffset, ref this.publicKeyIndexOffset, ref this.publicKeyOffset, ref this.assemblyFlag, ref this.assemblyFlagOffset, ref this.compiledRuntimeVersion, ref this.assemblyReferences, ref this.blobIndexSize))
            {
                foreach (AssemblyReference reference in this.assemblyReferences)
                {
                    //if (reference.ReferenceName.IndexOf("xtest") != -1)
                    {
                        ListViewItem item = new ListViewItem(new string[] { reference.ReferenceName, reference.Version, reference.PublicKeyOrToken, "0x" + reference.ReferenceOffset.ToString("X8") })
                        {
                            Tag = reference
                        };
                        this.lstReferences.Items.Add(item);
                    }
                }
                this.AppendMessage("程序集编译运行库: " + this.compiledRuntimeVersion, ImageListIcon.Info);
                this.AppendMessage("程序集CLI头标志: 0x" + this.cliHeaderFlag.ToString("X8") + " (" + Utility.CLIHeaderFlagToString(this.cliHeaderFlag) + ") 所在偏移位置: 0x" + this.cliHeaderFlagOffset.ToString("X8"), ImageListIcon.Info);
                this.AppendMessage("程序集表标志: 0x" + this.assemblyFlag.ToString("X8") + " (" + Utility.AssemblyFlagToString(this.assemblyFlag) + ") 偏移量: 0x" + this.assemblyFlagOffset.ToString("X8"), ImageListIcon.Info);
                if (this.strongNameSignatureOffset == 0L)
                {
                    this.AppendMessage("程序集强命名无效或不存在", ImageListIcon.Info);
                }
                else
                {
                    this.AppendMessage("程序集强命名CLI头索引偏移量: 0x" + this.strongNameSignatureOffset.ToString("X8"), ImageListIcon.Info);
                    if (this.publicKeyOffset > 0L)
                    {
                        this.AppendMessage("程序集公钥偏移量: 0x" + this.publicKeyOffset.ToString("X8"), ImageListIcon.Info);
                    }
                    if (this.publicKeyIndexOffset > 0L)
                    {
                        this.AppendMessage("公钥索引在程序集表中偏移量: 0x" + this.publicKeyIndexOffset.ToString("X8"), ImageListIcon.Info);
                    }
                }
                this.btnPatchReferences.Enabled = true;
                if (!Utility.IsAssemblyStrongSigned(this.cliHeaderFlag))
                {
                    this.AppendMessage("程序集中没有找到签名，不需要处理", ImageListIcon.Alert);
                    this.btnPatch.Enabled = false;
                }
                else
                {
                    this.btnPatch.Enabled = true;
                    if (string.Compare(Path.GetExtension(fileName), ".dll", true) == 0)
                    {
                        this.AppendMessage("Removing strong-signing from a DLL prevents main executable to load it. Patch it accordingly", ImageListIcon.Alert);
                    }
                }
            }
            else
            {
                this.AppendMessage("未能打开程序集，或不是有效的.NET 程序集", ImageListIcon.Error);
                this.btnPatchReferences.Enabled = false;
            }
        }

        private enum ImageListIcon
        {
            Error,
            Alert,
            Info,
            OK
        }
    }
}
